package cn.edu.buu.hospital.service.impl;

import cn.edu.buu.hospital.entity.User;
import cn.edu.buu.hospital.mapper.UserMapper;
import cn.edu.buu.hospital.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public void removeById(Long userid) {
        userMapper.deleteById(userid);
    }

    @Override
    public void modify(User user) {
        userMapper.update(user);
    }

    @Override
    public User findById(Long userid) {
        return userMapper.selectById(userid);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public PageInfo<User> find(int pageNum, int pageSize, User condition) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectByCondition(condition);
        return new PageInfo<>(userList);
    }

    @Override
    public User findAdmin(String username, String userpwd, Integer type) {
        return userMapper.selectAdmin(username, userpwd, type);
    }

    @Override
    public User findByUserName(String doctorName) {
        User condition = new User();
        condition.setUsername(doctorName);
        List<User> userList = userMapper.selectByCondition(condition);
        // 如果找到符合条件的用户，返回第一个
        if (userList != null && !userList.isEmpty()) {
            return userList.get(0);
        }
        return null;
    }
    
    @Override
    public User findByPhone(String phone) {
        User condition = new User();
        condition.setPhone(phone);
        List<User> userList = userMapper.selectByCondition(condition);
        if (userList != null && !userList.isEmpty()) {
            return userList.get(0);
        }
        return null;
    }
    
    @Override
    public long countDoctors() {
        return userMapper.countDoctors();
    }
}